package xyz.iguilty.workflow.core.listener;

import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.delegate.event.impl.FlowableProcessEventImpl;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import xyz.iguilty.workflow.constant.ProcessConstants;
import xyz.iguilty.workflow.core.service.AbstractActivity;
import xyz.iguilty.workflow.model.NextActors;
import xyz.iguilty.workflow.utils.ApplicationContextProvider;

import java.util.*;

@Component
@Deprecated
public class GlobalTaskListener extends AbstractFlowableEngineEventListener {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Deprecated
    @Override
    protected void processStarted(FlowableProcessStartedEvent event) {
        super.processStarted(event);
        FlowableProcessEventImpl eventImpl = (FlowableProcessEventImpl) event;
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(eventImpl.getProcessDefinitionId()).singleResult();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
        List<Process> processList = bpmnModel.getProcesses();
        Process process = processList.get(0);
        List<FlowElement> list = (List<FlowElement>) process.getFlowElements();
        Map<String, Object> variables = new HashMap<>();
        for(FlowElement flowElement : list){
            if(flowElement instanceof UserTask){
                UserTask userTask = (UserTask) flowElement;
                String currentActivityKey = userTask.getId();
                AbstractActivity activity = ApplicationContextProvider.getBean(currentActivityKey, AbstractActivity.class);
                if (activity == null) {
                    continue;
                }
                setNextActors(userTask,variables,activity);
            }
        }
        //将variables设置进去
        runtimeService.setVariables(eventImpl.getExecutionId(),variables);
    }

    private void setNextActors(UserTask userTask,Map<String, Object> variables,AbstractActivity activity){
        NextActors nextActors = activity.assignActor();
        if (nextActors==null){
            return;
        }
        variables.put(ProcessConstants.CURRENT_ACTOR,nextActors.getNameChain());
        String  inputDataItem;
        //判断是不是会签节点
        MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = userTask.getLoopCharacteristics();
        if(multiInstanceLoopCharacteristics != null){
            //会签节点
            //获取设置参数名称
            multiInstanceLoopCharacteristics.getInputDataItem();
            inputDataItem = multiInstanceLoopCharacteristics.getInputDataItem().replace("${","").replace("}","");
            //获取前端设置的用户或者组
            if(userTask.getCandidateGroups().size() > 0){
                //设置参数
                variables.put(inputDataItem,nextActors.getNextActors());
            }
        }else{
            //获取设置参数名称
            String assignee = userTask.getAssignee();
            if (!ObjectUtils.isEmpty(assignee)){
                if (assignee.contains("${")){
                    inputDataItem = assignee.replace("${","").replace("}","");
                    variables.put(inputDataItem,nextActors.getNextActors());
                }
            }
            if(userTask.getCandidateUsers().size() > 0){
                String candidateUser = userTask.getCandidateUsers().get(0);
                if (candidateUser.contains("${")){
                    inputDataItem = candidateUser.replace("${","").replace("}","");
                    variables.put(inputDataItem,nextActors.getNextActors());
                }
            }
            if(userTask.getCandidateGroups().size() > 0){
                String candidateGroup = userTask.getCandidateGroups().get(0);
                if (candidateGroup.contains("${")){
                    inputDataItem = candidateGroup.replace("${","").replace("}","");
                    variables.put(inputDataItem,nextActors.getNextActors());
                }
            }
        }
    }

}
